#include<bits/stdc++.h>
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;
typedef long long ll;
ll n, m, op[100005], c[100005], T, x[4], a[100005];
bool vis[105][105];
ll getK(ll x)
{
    ll cnt = 0;
    while(x)
        cnt++, x >>= 1LL;
    return cnt;
}
vector<ll> fun(ll R, ll G, ll C)
{
    bool flag = vis[R][G];
    ll l = (1 << R) * (G - 1) + 1, r = (1 << R) * G;
    vector<ll> v;
    if(R == 1)
    {

        if(l > C)
            return v.push_back(l), v.push_back(r), v;
        if(!flag)
        {
            if(a[l] >= R)
                return v.push_back(l), v;
            return v.push_back(r), v;
        }
        if(r > C)
            return v.push_back(l), v.push_back(r), v;
        if(a[r] >= R)
            return v.push_back(r), v;
        return v.push_back(l), v;
    }
    vector<ll> v1 = fun(R - 1, G * 2 - 1, C);
    vector<ll> v2 = fun(R - 1, G * 2, C);
    if(!flag)
    {
        for(ll i = 0; i < v1.size(); i++)
            if(v1[i] > C || a[v1[i]] < R)
            {
                for(ll i = 0; i < v2.size(); i++)
                    v.push_back(v2[i]);
                break;
            }
        for(ll i = 0; i < v1.size(); i++)
            if(v1[i] > C || a[v1[i]] >= R)
                v.push_back(v1[i]);
    }
    else
    {
        for(ll i = 0; i < v2.size(); i++)
            if(v2[i] > C || a[v2[i]] < R)
            {
                for(ll i = 0; i < v1.size(); i++)
                    v.push_back(v1[i]);
                break;
            }
        for(ll i = 0; i < v2.size(); i++)
            if(v2[i] > C || a[v2[i]] >= R)
                v.push_back(v2[i]);
    }
    return v;
}
int main()
{
    freopen("arena.in", "r", stdin);
    freopen("arena.out", "w", stdout);
    scanf("%lld%lld", &n, &m);
    for(ll i = 1; i <= n; i++)
        scanf("%lld", op + i);
    for(ll i = 1; i <= m; i++)
        scanf("%lld", c + i);
    ll t = getK(n);
    for(ll i = 1; i <= t; i++)
        for(ll j = 1; j <= (1LL << t - i); j++)
        {
            char ch = getchar();
            while(ch != '0' && ch != '1')
                ch = getchar();
            vis[i][j] = (ch == '1');
        }
    scanf("%lld", &T);
    while(T--)
    {
        for(ll i = 0; i < 4; i++)
            scanf("%lld", x + i);
        for(ll i = 1; i <= n; i++)
            a[i] = (op[i] ^ x[i % 4]);
        ll ans = 0;
        for(ll i = 1; i <= m; i++)
        {
            ll k = getK(c[i]);
            vector<ll> v = fun(k, 1, c[i]);
            ll num = 0;
            for(ll i = 0; i < v.size(); i++)
                num += v[i];
            ans ^= (num * i);
        }
        printf("%lld\n", ans);
    }
    return 0;
}
